<?php
class Main_RelationController extends Zend_Controller_Action
{
    protected $_model;
    protected $_authService;

    public function init()
    {
        $layout = $this->_helper->layout();
        $layout->setLayout('main');

        $this->_model = new Main_Model_Relation();

        $this->_authService = new Main_Service_Authentication();
        // add forms
        $this->view->relationForm = $this->getRelationForm();
    }
    /*
     * lists all the relations
     */
    public function indexAction()
    {
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

        $this->view->relations = $this->_model->getAllRelations();
    }

    public function newAction()
    {
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

        $this->_helper->FlashMessenger('')->clearMessages();
        $this->_helper->FlashMessenger('')->setNamespace('header')->addMessage('New Relation');
        $this->_forward('edit');
    }

    /**
     * Used to show the relation edit/create form
     */
    public function editAction()
    {
         if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');
        
        $requst_params = $this->getRequest()->getParams();
      // if id is in request param && user is admin && is logged in
        if( isset($requst_params['id']) && $requst_params['id'] !='' )
        {
            $this->_helper->FlashMessenger('')->clearMessages();
            $this->_helper->FlashMessenger('')->setNamespace('header')->addMessage('Edit Relation');
            $id  =  $requst_params['id'];
            $this->view->relation = $this->_model->getRelationById($id);

            $this->view->relationForm = $this->getRelationForm()->populate( $this->view->relation->toArray() );
        }
    }

    public  function saveAction()
    {
        // echo '<pre>'; print_r($_REQUEST); echo '</pre>';
        $request = $this->getRequest();
        if (!$request->isPost()) {
            return $this->_helper->redirector('index');
        }
        // echo '<br> saving relation saveAction';
        # @TODO:: validate here & then save
        if (false === $this->_model->saveRelation($request->getPost())  )
        {
            $this->_helper->FlashMessenger('')->setNamespace('error')->addMessage('An error occured while saving relation');

            return $this->render('/edit-relation');
        }

        $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('Relation info saved');
        return $this->_redirect('/manage-relations');
    }

    public function deleteAction()
    {
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

         if (false === ($id = $this->_getParam('id',false))) {
            throw new WOW_Exception('Unknown Relation');
        }

        if($this->_model->deleteRelation($id))
            $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('Relation Deleted');
        else
            $this->_helper->FlashMessenger('')->setNamespace('Error')->addMessage('An error occured');

        return $this->_redirect('/manage-relations');
    }


    public function getRelationForm()
    {
        return $this->_forms['relationform'] = $this->_model->getForm('RelationRelationform');
    }
}